﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;

namespace KouDai.Phone.Helper
{
    public static class ParamHelper
    {
        public static string BuildParas(Dictionary<string, object> paras)
        {
            if (paras == null || paras.Count <= 0)
            {
                return "";
            }

            StringBuilder sb = new StringBuilder();
            foreach (KeyValuePair<string, object> param in paras)
            {
                string key = param.Key;
                object value = param.Value;
                Type type = value.GetType();
                sb.Append(key);
                sb.Append("=");
                if ("String".Equals(type.Name))
                {
                    sb.Append(value);
                }
                else if ("Int32".Equals(type.Name))
                {
                    sb.Append(Convert.ToInt32(value));
                }
                else if ("Int64".Equals(type.Name))
                {
                    sb.Append(Convert.ToInt64(value));
                }
                sb.Append("&");
            }
            return sb.ToString().Substring(0, sb.Length - 1);
        }
        /// <summary>
        /// 根据参数集合获取值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="body"></param>
        /// <returns></returns>
        public static T GetParam<T>(string key, string body)
        {
            if (body.Contains("?"))
            {
                body = body.Substring(body.IndexOf("?") + 1);
            }
            object value;
            Dictionary<string, object> dic = new Dictionary<string, object>();
            string[] andColl = body.Split('&');
            foreach (string str in andColl)
            {
                string[] equColl = str.Split('=');
                dic.Add(equColl[0], equColl[1]);
            }
            dic.TryGetValue(key, out value);
            return (T)value;
        }

        public static T GetParamEx<T>(string key, string body)
        {
            if (body.Contains("?"))
            {
                body = body.Substring(body.IndexOf("?") + 1);
            }
            object value;
            Dictionary<string, object> dic = new Dictionary<string, object>();
            string[] andColl = body.Split('&');
            foreach (string str in andColl)
            {
                if (str.Contains(key))
                {
                    string keyValue = str.Substring(body.IndexOf("=") + 1);
                    dic.Add(key, keyValue);
                }
            }
            dic.TryGetValue(key, out value);
            return (T)value;
        }

        public static string EncryptParam(Dictionary<string, object> header, Dictionary<string, object> body)
        {
            Dictionary<string, object> allDic = new Dictionary<string, object>();
            allDic.Add("header", header);
            allDic.Add("body", body);
            //Debug.WriteLine(JsonConvert.SerializeObject(allDic));
            return "edata=" + HttpUtility.UrlEncode(EncryptHelper.AESEncrypt(JsonConvert.SerializeObject(allDic), "00118c9b7604620d7c73f720639de613", null));
        }
    }
}
